function environment(lambda, chi, phi, y1, y2, D0, R1, delta, delta_grid, dist_d, d_min, d_max, R1_grid, D0i_grid, s, optfig)
% This function illustrates the partial equilibrium of the model

%% delta_grid

tax    = zeros(size(delta_grid)); % T(s)
alphaF = zeros(size(delta_grid));
CF_del = zeros(length(delta_grid),length(D0i_grid)); % Consumption in case of failure, given delta
for i=1:length(delta_grid)
    delta_i         = delta_grid(i);
    
    tax(i)          = fn_tax(delta_i,R1,D0,chi,s,phi,dist_d,d_min,d_max);
    alphaF(i)       = fn_alphaF(delta_i,R1,D0,chi,s,phi,dist_d,d_min,d_max);
    for j=1:length(D0i_grid)
        D0i         = D0i_grid(j);
        CF_del(i,j) = fn_CF(delta,R1,D0i,y1,alphaF(i)); % For early types, since we use y=y1
    end
end

fig_delta_grid(delta_grid, tax, CF_del, alphaF, D0i_grid, s, optfig)

%% R1_grid

CF        = zeros(length(R1_grid), length(D0i_grid));
C1N       = zeros(length(R1_grid), length(D0i_grid));
C2N       = zeros(length(R1_grid), length(D0i_grid));
alphaF_R1 = zeros(size(R1_grid));
for i=1:length(R1_grid)
    R1_i         = R1_grid(i);
    alphaF_R1(i) = fn_alphaF(delta,R1_i,D0,chi,s,phi,dist_d,d_min,d_max);
    
    for j=1:length(D0i_grid)
        D0i      = D0i_grid(j);
        
        CF(i,j)  = fn_CF(delta,R1_i,D0i,y1,alphaF_R1(i)); % For early types, since we use y=y1
        C1N(i,j) = fn_C1N(R1_i,D0i,y1); % Early
        C2N(i,j) = fn_C2N(R1_i,D0i,lambda,y2,s,phi); % Late
    end
end

fig_R1_grid(R1_grid, CF, C1N, C2N, alphaF_R1, D0i_grid, s, optfig)

%% Shared of Insured/Uninsured

ins_dep   = zeros(size(delta_grid));
unins_dep = zeros(size(delta_grid));
ins_acc   = zeros(size(delta_grid));
unins_acc = zeros(size(delta_grid));

for i=1:length(delta_grid)
    delta_i  = delta_grid(i);
    
    [ins_dep(i), unins_dep(i)] = fn_insured_deposits(delta_i,R1,dist_d,d_min,d_max);
    [ins_acc(i), unins_acc(i)] = fn_insured_accounts(delta_i,R1,dist_d,d_min,d_max);
end

fig_insured(delta_grid, ins_dep, unins_dep, ins_acc, unins_acc, optfig)

end
